<?php
	require('member.doc.php');
	//验证手机号
	$app->get(
		'/member/mobile/check',
		function() use($app, $db2){
			$data = $app->request()->get();
			$echo['error'] = "";
			if($data && isset($data['mobile'])){
				$echo['status'] = false;
				$member = $db2->select(1, 'tb_member', '*', array('mobile' => $data['mobile']));
				if($data['mobile']!=1 && $data['type']==0){
					$coach = $db2->select(1, 'tb_coach', '*', array('mobile' => $data['mobile']));
				}
				if($member || $coach){
					$echo['status'] = true;
				}
			}else{
				$echo['error'] = "缺少参数，请检查";
			}
			echo json_encode($echo);
		}
	);
	//注册
	$app->post(
		'/member/register',
		function() use($app, $db2){
			$data = $app->request()->post();
			$echo['error'] = "";
			if($data && isset($data['mobile']) && isset($data['password']) && isset($data['captcha'])){
				if(!$db2->select(1, 'tb_member', '*', array('mobile' => $data['mobile'])) && !$db2->select(1, 'tb_coach', '*', array('mobile' => $data['mobile']))){
					if(checkCaptcha($data['mobile'], $data['captcha'], 11)){
						$set = array(
							'mobile' => $data['mobile'],
							'password' => $data['password'],
							'nickname' => '手机用户'.$data['mobile'],
							'dt' => date('Y-m-d H:i:s')
						);
						$echo['memberid'] = $db2->insert('tb_member', $set);
					}else{
						$echo['error'] = "验证码错误";
					}
				}else{
					if($db2->select(2, 'tb_member', '*', array('mobile' => $data['mobile']))!=0){
						$echo['error'] = "手机号已注册";
					}else{
						$echo['error'] = "手机号已注册教练版";
					}
				}
			}else{
				$echo['error'] = "数据缺失，请检查";
			}
			echo json_encode($echo);
		}
	);
	//登录
	$app->post(
		'/member/login',
		function() use($app, $db2){
			$data = $app->request()->post();
			$echo['error'] = "";
			if($data && isset($data['mobile'])){
				$member = $db2->select(1, 'tb_member', '*', array('mobile' => $data['mobile']));
				if($member){
					if(isset($data['password']) && $member['password'] == $data['password']){
						$echo['memberid'] = $member['tbid'];
						$echo['head'] = file_exists('dofiles/'.$member['tbid'].'/head/head.jpg') ? SITEROOTURL.'dofiles/'.$member['tbid'].'/head/head.jpg' : SITEROOTURL.'sqinc/skin/default/hdicon.png';
						$echo['mobile'] = $member['mobile'];
						$echo['nickname'] = $member['nickname'];
						$echo['balance'] = $member['balance'];
						$echo['issetpaypassword'] = $member['paypassword'] ? true : false;
					}else{
						$echo['error'] = "登录密码错误";
					}
				}else{
					if(!$db2->select(2, 'tb_coach', '*', array('mobile' => $data['mobile']))!=0){
						$echo['error'] = "手机号不存在";
					}else{
						$echo['error'] = "手机号已注册教练版";
					}
				}
			}else{
				$echo['error'] = "数据缺失，请检查";
			}
			echo json_encode($echo);
		}
	);
	//找回密码
	$app->post(
		'/member/password/find',
		function() use($app, $db2){
			$data = $app->request()->post();
			$echo['error'] = "";
			if($data && isset($data['mobile']) && isset($data['password']) && isset($data['captcha'])){
				if(checkCaptcha($data['mobile'], $data['captcha'], 12)){
					$db2->update('tb_member', array('password' => $data['password']), array('mobile' => $data['mobile']));
				}else{
					$echo['error'] = "验证码错误";
				}
			}else{
				$echo['error'] = "数据缺失，请检查";
			}
			echo json_encode($echo);
		}
	);
	//修改密码
	$app->post(
		'/member/password/update',
		function() use($app, $db2){
			$data = $app->request()->post();
			$echo['error'] = "";
			if($data && isset($data['memberid']) && isset($data['oldpassword']) && isset($data['newpassword'])){
				$m = $db2->select(1, 'tb_member', '*', array('tbid' => $data['memberid']));
				if($m['password'] == $data['oldpassword']){
					$db2->update('tb_member', array('password' => $data['newpassword']), array('tbid' => $data['memberid']));
					XingeApp::PushAccountAndroid(XG_ANDROID_ACCESS_ID, XG_ANDROID_SECRET_KEY, '密码修改通知', '你的登录密码已修改', 'gym'.strval($data['memberid']));
				}else{
					$echo['error'] = "原密码错误";
				}
			}else{
				$echo['error'] = "数据缺失，请检查";
			}
			echo json_encode($echo);
		}
	);
	//修改交易密码
	$app->post(
		'/member/paypassword/update',
		function() use($app, $db2){
			$data = $app->request()->post();
			$echo['error'] = "";
			if($data && isset($data['memberid']) && isset($data['oldpaypassword']) && isset($data['newpaypassword'])){
				$m = $db2->select(1, 'tb_member', '*', array('tbid' => $data['memberid']));
				if($m['paypassword'] == $data['oldpaypassword'] || !$m['paypassword']){
					$db2->update('tb_member', array('paypassword' => $data['newpaypassword']), array('tbid' => $data['memberid']));
				}else{
					$echo['error'] = "原支付密码错误";
				}
			}else{
				$echo['error'] = "数据缺失，请检查";
			}
			echo json_encode($echo);
		}
	);
	//修改交易密码（通过短信验证码）
	$app->post(
		'/member/paypassword/updatebycaptcha',
		function() use($app, $db2){
			$data = $app->request()->post();
			$echo['error'] = "";
			if($data && isset($data['memberid']) && isset($data['paypassword']) && isset($data['captcha'])){
				$m = $db2->select(1, 'tb_member', '*', array('tbid' => $data['memberid']));
				if(checkCaptcha($m['mobile'], $data['captcha'], 13)){
					$db2->update('tb_member', array('paypassword' => $data['paypassword']), array('tbid' => $data['memberid']));
				}else{
					$echo['error'] = "验证码错误";
				}
			}else{
				$echo['error'] = "数据缺失，请检查";
			}
			echo json_encode($echo);
		}
	);
	//验证交易密码
	$app->post(
		'/member/paypassword/check',
		function() use($app, $db2){
			$data = $app->request()->post();
			$echo['error'] = "";
			if($data && isset($data['memberid']) && isset($data['paypassword'])){
				$echo['status'] = false;
				if($db2->select(1, 'tb_member', '*', array('tbid' => $data['memberid'], 'paypassword' => $data['paypassword']))){
					$echo['status'] = true;
				}
			}else{
				$echo['error'] = "数据缺失，请检查";
			}
			echo json_encode($echo);
		}
	);
	//查询用户信息
	$app->get(
		'/member/info',
		function() use($app, $db2){
			$data = $app->request()->get();
			$echo['error'] = "";
			if($data && isset($data['memberid'])){
				$member = $db2->select(1, 'tb_member', '*,(case when date_format(privilege_dt,"%Y-%m-%d")>=curdate() then 1 else 0 end) as isprivilege', array('tbid' => $data['memberid']));
				if($member){
					$echo['memberid'] = $member['tbid'];
					$echo['head'] = file_exists('dofiles/member/'.$member['tbid'].'/head/head.jpg') ? SITEROOTURL.'dofiles/member/'.$member['tbid'].'/head/head.jpg' : SITEROOTURL.'sqinc/skin/default/hdicon.png';
					$echo['mobile'] = $member['mobile'];
					$echo['nickname'] = $member['nickname'];
					$echo['balance'] = $member['balance'];
					$echo['adaa'] = $member['adaa'];
					$echo['sign'] = $db2->select(2, 'tb_sign', '*', array(
						'userid' => $member['tbid'],
						'usertype' => 1,
						'type' => 1
					));
					$echo['issetpaypassword'] = $member['paypassword'] ? true : false;
					$echo['sex'] = $member['sex'];
					$echo['realname'] = $member['realname'];
					$echo['idcard'] = $member['idcard'];
					$echo['photo'] = SITEROOTURL.$member['photo'];
					$echo['state'] = $member['state'];
					$echo['isprivilege'] = $member['isprivilege'];
					if(!empty($member['privilege_dt'])){
						$echo['privilege_dt'] = date('Y-m-d',strtotime($member['privilege_dt']));
					}else{
						$echo['privilege_dt'] ='';
					}
				}else{
					$echo['error'] = "用户不存在";
				}
			}else{
				$echo['error'] = "数据缺失，请检查";
			}
			echo json_encode($echo);
		}
	);
	//修改昵称
	$app->post(
		'/member/nickname/update',
		function() use($app, $db2){
			$data = $app->request()->post();
			$echo['error'] = "";
			if($data && isset($data['memberid']) && isset($data['nickname'])){
				$db2->update('tb_member', array('nickname' => $data['nickname']), array('tbid' => $data['memberid']));
			}else{
				$echo['error'] = "数据缺失，请检查";
			}
			echo json_encode($echo);
		}
	);
	//修改头像
	$app->post(
		'/member/head/update',
		function() use($app, $db2){
			$data = $app->request()->post();
			$echo['error'] = "";
			if($data && isset($data['memberid']) && isset($_FILES['head'])){
				$headpath = 'dofiles/member/'.$data['memberid'].'/head/';
				create($headpath);
				$headurl = $headpath.'head.jpg';
				move_uploaded_file($_FILES['head']['tmp_name'], $headurl);
			}else{
				$echo['error'] = "数据缺失，请检查";
			}
			echo json_encode($echo);
		}
	);
	//会员真实信息填写
	$app->post(
		'/member/realinfo/update',
		function() use($app, $db2){
			$data = $app->request()->post();
			$echo['error'] = "";
			if($data && isset($data['memberid']) && isset($data['realname']) && isset($data['idcard']) && isset($data['sex']) && isset($_FILES['photo'])){
				if(!$db2->select(1, 'tb_coach', '*', array('idcard' => $data['idcard'])) && !$db2->select(1, 'tb_member', '*', array('idcard' => $data['idcard'],'tbid[!]'=>$data['memberid']))){
					$photopath = 'dofiles/member/'.$data['memberid'].'/photo/';
					create($photopath);
					$photourl = $photopath.date('YmdHis').'.jpg';
					copy($_FILES['photo']['tmp_name'], $photourl);
					$db2->update('tb_member', array(
						'realname' => $data['realname'],
						'idcard' => $data['idcard'],
						'sex' => $data['sex'],
						'photo' => $photourl,
						'state' => 2
					), array(
						'tbid' => $data['memberid']
					));
				}else{
					$echo['error'] = "身份证号已存在";
				}
			}else{
				$echo['error'] = "数据缺失，请检查";
			}
			echo json_encode($echo);
		}
	);
?>
